/****************************************************************************************/
/*                                                                     					*/
/*  M16C/62P Group Program Collection                                  					*/
/*                                                                     					*/
/*  FILE NAME : delay_timer.c                                       					*/
/*  CPU       : This program is the Serial flash boot loader by Xmodem data transfer	*/
/*  HISTORY   : 2006.12.08 Ver 0.01                                    					*/
/*                                                                    	 				*/
/*  Copyright (C) 2006. Renesas Technology Corp.                       					*/
/*  Copyright (C) 2006. Renesas Solutions Corp.                        					*/
/*  All right reserved.                                                					*/
/*                                                                     					*/
/*****************************************************************************************/

/**************************************/
/*    include file                    */
/**************************************/
#include "delay_timer.h"

#include"sfr62p.h"

/**************************************/
/*    static variable declaration     */
/**************************************/
static unsigned char DelayTimerUnderFlowFlag = 0;

/**************************************/
/*    InitDelayTimer function         */
/**************************************/
void InitDelayTimer (void)
{
	// stop the timer channel
	ta0s = 0;								//	0 - stops counting
											//	1 - starts counting
	
	// Timer A mode register
											//	operation mode select bits
											//	TM0D1	TM0D0
	tmod0_ta0mr = 0;						//	0		0			- timer mode
	tmod1_ta0mr = 0;						//	0		1			- event counter mode
											//	1		0			- one-shot timer mode
											//	1		1			- pwm mode
											
											//	pulse output function bit
	mr0_ta0mr = 0;							// 	0 - pulse not output
											//	1 - pulse output
	
											//	gate function select bits
											//	MR2	MR1	MR0
	mr1_ta0mr = 0;							//	0	0	0			- gate function not available
											//	0	0	1			- 
											//	0	1	0
											//	0	1	1
											//	1	0	0
											//	1	0	1
											//	1	1	0
											//	1	1	1
											
	mr2_ta0mr = 0;				// "
	mr3_ta0mr = 0;				// must be '0' in timer mode
	tck0_ta0mr = 0;				// clock = f32
	tck1_ta0mr = 1;				// "
	
	// timer counts down
	udf = 0x00;
	
	// interrupt priority level
	ta0ic = 5;


}

#pragma SECTION program ram_based_prg

/**************************************/
/*    StartDelayTimer function        */
/**************************************/
void StartDelayTimer (void)
{
	DelayTimerUnderFlowFlag = 0;
	ta0 = ONE_MSEC - 1;				// set initial count value
	
	ta0s = 1;		 				// start timer
}

/**************************************/
/*    StopDelayTimer function         */
/**************************************/
void StopDelayTimer (void)
{
	ta0s = 0;						// stop timer
}

/**************************************/
/*    GetDelayTimerStatus function    */
/**************************************/
unsigned char GetDelayTimerStatus (void)
{
	if ( DelayTimerUnderFlowFlag == 1 )
	{
		return (1);
	}			   
	else
	{
		return (0);
	}
}

/**************************************/
/*    TimerA0Interrupt function       */
/**************************************/
void TimerA0Interrupt(void)
{
   DelayTimerUnderFlowFlag = 1;
   StopDelayTimer();
}
